package com.tjcd.project.library_base.base.adapter.listview;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import java.util.List;

/**
 * @Author jiangKunKun
 * @CreateDate 2021/7/8
 * @Description
 * @SpittingGrooves
 */
public abstract class CommonAdapter<T> extends BaseAdapter {
    /**
     * 数据
     */
    protected List<T> mDates;
    /**
     * 上下文
     */
    protected Context context;
    /**
     * 布局id
     */
    protected final int mLayoutId;

    /**
     * 创建构造方法
     * @param context
     * @param mLayoutId
     * @param mDates
     */
    public CommonAdapter(Context context, int mLayoutId, List<T> mDates) {
        this.mDates = mDates;
        this.context = context;
        this.mLayoutId = mLayoutId;
    }

    @Override
    public int getCount() {
        return mDates.size();
    }

    @Override
    public T getItem(int position) {
        return mDates.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // 获取viewHolderHelper
        ViewHolderHelper viewHolderHelper = ViewHolderHelper
                .getViewHolderHelper(context, parent, convertView, mLayoutId,
                        position);
        convert(viewHolderHelper, getItem(position), position);
        return viewHolderHelper.getConvertView();
    }

    /**
     * 用于初始化viewHolderHelper中控件
     * @param holder
     * @param item
     * @param position
     */
    public abstract void convert(ViewHolderHelper holder, T item,
                                 int position);

}
